package com.wgw.first.home.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wgw.first.R;
import com.wgw.first.base.BaseFragment;
import com.wgw.first.dao.HomeBean;
import com.wgw.first.home.adapter.HomeFragmentAdapter;
import com.wgw.first.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class homeFragment extends BaseFragment {


    private RecyclerView rvHome;
    private TextView et_search_home;
    private ImageButton ib_message_home;
    private HomeFragmentAdapter adapter;
    private HomeBean homeBean;

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.home_fragment,null);
        rvHome = (RecyclerView) view.findViewById(R.id.rv_home);
        et_search_home = (EditText) view.findViewById(R.id.et_search);
        ib_message_home = (ImageButton) view.findViewById(R.id.ib_message);

        return view;
    }



    @Override
    public void initData() {
        getDataFromNet();
    }
    public void getDataFromNet() {
        OkHttpUtils
                .get()
                .url(Constants.BASE+Constants.HOMEJSON)
                //.url("http://192.168.191.1:8080/Product/HomeServlet")
                .id(100)
                .build()
                .execute(new MyStringCallback());
    }
    public class MyStringCallback extends StringCallback {

        @Override
        public void onError(Call call, Exception e, int id) {
            Log.e("TAG", "联网失败" + e.getMessage());
        }

        @Override
        public void onResponse(String response, int id) {

            switch (id) {
                case 100:
                    if (response != null) {

                        //对response进行解析
                        processData(response);

                        //设置recyclerview的适配器
                        adapter = new HomeFragmentAdapter(mContext,homeBean);
                        rvHome.setAdapter(adapter);
                        GridLayoutManager manager = new GridLayoutManager(getActivity(),1);
                        rvHome.setLayoutManager(manager);

                        initListener();
                    }
                    break;
                case 101:
                    //                    Toast.makeText(mContext, "https", Toast.LENGTH_SHORT).show();
                    break;
            }
        }

        

    }
    //对json数据进行解析

    private void processData(String json) {

        if (!TextUtils.isEmpty(json)) {
            JSONObject HomeBean = JSON.parseObject(json, com.wgw.first.dao.HomeBean.class);
            String banner_info = HomeBean.getString("banner_info");
            String exclusive_info = HomeBean.getString("exclusive_info");
            String goodbook_info = HomeBean.getString("goodbook_info");
            String newbook_info = HomeBean.getString("newbook_info");

            homeBean = new HomeBean();

            //设置banner数据
            List<com.wgw.first.dao.HomeBean.BannerInfoBean> bannerInfoBeen = JSON.parseArray(banner_info, com.wgw.first.dao.HomeBean.BannerInfoBean.class);
            homeBean.setBanner_info(bannerInfoBeen);

            //设置newbook数据
            List<com.wgw.first.dao.HomeBean.NewbookInfoBean> newbookInfoBeen = JSON.parseArray(newbook_info, com.wgw.first.dao.HomeBean.NewbookInfoBean.class);
            homeBean.setNewbook_info(newbookInfoBeen);

            //设置exclusivebook数据
            List<com.wgw.first.dao.HomeBean.ExclusiveInfoBean> exclusiveInfoBeen = JSON.parseArray(exclusive_info, com.wgw.first.dao.HomeBean.ExclusiveInfoBean.class);
            homeBean.setExclusive_info(exclusiveInfoBeen);

            //设置goodbook数据
            List<com.wgw.first.dao.HomeBean.GoodbookInfoBean> goodbookInfoBeen = JSON.parseArray(goodbook_info, com.wgw.first.dao.HomeBean.GoodbookInfoBean.class);
            homeBean.setGoodbook_info(goodbookInfoBeen);


        }

    }

    private void initListener() {
    }


}
